package com.zhuhaibaoxian.wanjiuseller.view.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.BuildBean;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhuhaibaoxian.wanjiuseller.IView.IListView;
import com.zhuhaibaoxian.wanjiuseller.R;
import com.zhuhaibaoxian.wanjiuseller.adapter.MyRecycleViewAdapter;
import com.zhuhaibaoxian.wanjiuseller.app.MyApplication;
import com.zhuhaibaoxian.wanjiuseller.bean.ReceiveListBean;
import com.zhuhaibaoxian.wanjiuseller.bean.RemarkBean;
import com.zhuhaibaoxian.wanjiuseller.bean.SureListBean;
import com.zhuhaibaoxian.wanjiuseller.interfacewj.GetListPresenter;
import com.zhuhaibaoxian.wanjiuseller.utils.ToastUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RemarkActivity extends AppCompatActivity implements IListView {

    @Bind(R.id.toolbar_title)
    TextView toolbarTitle;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.btn_update)
    Button btnUpdate;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tab)
    TabLayout tab;
    @Bind(R.id.recyclerview)
    XRecyclerView recyclerview;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    private GetListPresenter getListPresenter;
    private BuildBean buildBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remark);
        ButterKnife.bind(this);
        recyclerview.setLoadingMoreEnabled(false);
        getListPresenter = new GetListPresenter(this);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getData(recyclerview);
            }

            @Override
            public void onLoadMore() {

            }
        });
        getData(recyclerview);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData(XRecyclerView recyclerview) {
        buildBean = DialogUIUtils.showLoading(RemarkActivity.this, "加载中...", false, true, true, true);
        buildBean.show();
        getListPresenter.getRemarkList(recyclerview, buildBean);
    }

    @Override
    public void getDataSuccess(ReceiveListBean value) {

    }

    @Override
    public void sureSuccess(SureListBean value) {

    }

    @Override
    public void getRemarkSuccess(final RemarkBean value) {
        if (value.getData() != null && value.getData().size() > 0) {
            recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            //noinspection unchecked
            recyclerview.setAdapter(new MyRecycleViewAdapter(value.getData(), R.layout.item_remarklist, this, false) {
                @Override
                protected void setPositionClick(int position) {
                }

                @Override
                protected void initData(MyViewHolder holder, int position) {
                    List<RemarkBean.DataBean> w = value.getData();
                    setHolderData(w, holder, position);
                }

            });
            recyclerview.refreshComplete();
            DialogUIUtils.dismiss(buildBean);

        } else {
            DialogUIUtils.dismiss(buildBean);
            ToastUtil.showLong(MyApplication.context, "没有留言哦！");
        }
    }

    private void setHolderData(List<RemarkBean.DataBean> w, MyRecycleViewAdapter.MyViewHolder holder, int position) {
        holder.getTextView(R.id.tv_mobile).setText(w.get(position).getLogin_id() == null ? "" : w.get(position).getLogin_id());//留言手机号
        holder.getTextView(R.id.tv_address).setText(w.get(position).getAddress() == null ? "" : w.get(position).getAddress());//留言地址
        holder.getTextView(R.id.tv_time).setText(w.get(position).getTime() == null ? "" : w.get(position).getTime());//留言时间
        holder.getTextView(R.id.tv_content).setText(w.get(position).getRemarks() == null ? "" : w.get(position).getRemarks());//留言内容
    }
}
